package org.example;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;

import java.util.List;

public class NacosConfig {

    public static String getStatus(String serverName) {
        String serverAddr = "localhost:8848"; // Nacos服务器地址
        StringBuilder result = new StringBuilder();

        try {
            // 创建NamingService实例
            NamingService namingService = NacosFactory.createNamingService(serverAddr);

            // 获取服务实例列表
            List<Instance> instances = namingService.getAllInstances(serverName);

            result.append("Nacos服务状态:\n");
            result.append("服务名: ").append(serverName).append("\n");
            result.append("实例数量: ").append(instances.size()).append("\n");

            for (Instance instance : instances) {
                result.append("---------------------\n");
                result.append("实例IP: ").append(instance.getIp()).append("\n");
                result.append("实例端口: ").append(instance.getPort()).append("\n");
                result.append("健康状态: ").append(instance.isHealthy() ? "健康" : "不健康").append("\n");
                result.append("元数据: ").append(instance.getMetadata()).append("\n");
            }

        } catch (NacosException e) {
            result.append("获取Nacos服务状态失败: ").append(e.getMessage());
        }

        return result.toString();
    }
}
